package org.jeecg.modules.sistersophie.job;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.modules.sistersophie.entity.StudentInfo;
import org.jeecg.modules.sistersophie.facade.StudentFacade;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * 每年9月1日 年级自动+1
 * 美国东部 (俄亥俄州)us-east-2
 */
@Slf4j
public class StudentGradeUpdateJob implements Job {

    @Autowired
    private StudentFacade studentFacade;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        JobDataMap jobDataMap = jobExecutionContext.getMergedJobDataMap();
        if(jobDataMap != null){
            String mapKey = "parameter";
            String executeDate = jobDataMap.getString(mapKey);
            if(StringUtils.isNotBlank(executeDate)){
                String currentDate = DateUtil.format(new Date(),"MM-dd");
                log.info("executeDate：{},currentDate：{}",executeDate,currentDate);
                if(executeDate.equals(currentDate)){
                    String currentTime = DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN);
                    //加载学生列表
                    QueryWrapper<StudentInfo> queryWrapper = new QueryWrapper<StudentInfo>();
                    queryWrapper.eq("is_delete",0).isNotNull("grade").ne("grade","");
                    //queryWrapper.and(wrapper -> wrapper.isNull("modify_grade_time").or().lt("modify_grade_time",currentTime));
                    List<StudentInfo> studentInfoList = studentFacade.selectStudentList(queryWrapper);
                    if(studentInfoList != null && studentInfoList.size() > 0){
                        for(StudentInfo info : studentInfoList){
                            try{
                                String grade = info.getGrade();
                                int preGrade = Integer.parseInt(grade);
                                int nextGrade = preGrade + 1;
                                if(nextGrade == 0){
                                    nextGrade = 1;
                                }else if(nextGrade > 13){
                                    continue;
                                }
                                info.setPreGrade(String.valueOf(preGrade));
                                info.setGrade(String.valueOf(nextGrade));
                                info.setModifyGradeTime(new Date());
                                studentFacade.modifyStudent(info);
                            }catch (Exception e){
                                e.printStackTrace();
                            }
                        }
                    }
                }
            }
        }
    }
}
